﻿using Abp.Domain.Repositories;
using AppFramework.DinosaursParkMes.CodingRuleModel;
using Microsoft.Extensions.Hosting;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;

namespace AppFramework.DinosaursParkMes.DomainService.CodingRule.TimeCoding
{
    public class CodingPush: BackgroundService
    {
        //依赖注入
        private readonly IRepository<Codingrules> _repository;
        private Timer _timer;

        //构造方案
        public CodingPush(IRepository<Codingrules> repository)
        {
            _repository = repository;
        }
        //执行任务
        protected override Task ExecuteAsync(CancellationToken stoppingToken)
        {
            _timer = new Timer(Dowork, null, TimeSpan.Zero, TimeSpan.FromMinutes(3));//定时器创建后马上执行，每天进行执行一次
            return Task.CompletedTask;
        }
        /// <summary>
        /// 定时器具体执行的方法
        /// </summary>
        /// <param name="state"></param>
        private async void Dowork(object state)
        {
            //查询表中的类型为每天的类型
            var list = await _repository.GetAllListAsync();
            foreach (var item in list)
            {
                //进行赋值
                item.Current_Number = 0;
                //进行修改
                Console.WriteLine(_repository.Update(item));
            }
        }
        /// <summary>
        /// 释放资源
        /// </summary>
        public override void Dispose()
        {
            _timer?.Dispose(); //如果_timer对象不为null，则销毁
            base.Dispose();
        }
    }
}
